<template>
  <div class="mod-config">
    <el-form :inline="true" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input type="small" v-model="keyword" placeholder="请输入标题名" clearable></el-input>
        <!--        <el-input v-model="keyword" placeholder="" clearable></el-input>-->
      </el-form-item>
      <el-form-item>
        <el-input type="small" v-model="keyword" placeholder="课程状态" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">搜索</el-button>
      </el-form-item>
      <div>
        <el-form-item>
          <!--          <drawer/>-->
          <el-button type="small" @click="addOrCurriculum()">课程新增</el-button>
        </el-form-item>
        <el-form-item>
          <!--        <el-button type="small" @click="getDataList()">搜索</el-button>-->
          <!--        <el-button  v-if="isAuth('news:newsCategory:save')" type="small" @click="addOrUpdateHandle()">新增课程-->
          <!--        </el-button>-->
          <el-button
            v-if="isAuth('news:newsCategory:delete')"
            type="small"
            @click="deleteHandle()"
            :disabled="dataListSelections.length <= 0"
          >批量删除</el-button>
        </el-form-item>
      </div>
    </el-form>
    <el-table
      :data="dataList"
      row-key="id"
      border
      size="mini"
      height="540"
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;"
    >
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column align="center" type="index" header-align="center" width="80px" label="序号"></el-table-column>
      <el-table-column prop="title" header-align="center" align="center" label="课程名称"></el-table-column>
      <el-table-column prop="cateName" label="课程分类" header-align="center" align="center"></el-table-column>
      <el-table-column prop="startTime" label="开始时间" header-align="center" align="center"></el-table-column>
      <el-table-column prop="endTime" label="结束时间" header-align="center" align="center"></el-table-column>
      <el-table-column prop="classStatus" label="课程状态" header-align="center" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.classStatus === 0">未发布</el-tag>
          <el-tag v-if="scope.row.classStatus === 1">已发布</el-tag>
          <el-tag v-if="scope.row.classStatus === 2">取消发布</el-tag>
          <el-tag v-if="scope.row.classStatus === 3">课程下架</el-tag>
        </template>
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="200" label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="addOrCurriculum(scope.row.id)">修改</el-button>
          <el-button type="text" @click="commentHandle(scope.row.id)">查看评价</el-button>
          <!--          <el-button type="text" @click="Handle(scope.row.id)">确认发布</el-button>-->
          <el-button type="text" plain @click="drawer = true" style="margin-left: 16px;">重新发布</el-button>
          <el-button type="text" plain @click="handleClick" style="margin-left: 16px;">取消发布</el-button>
          <el-button type="text" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-drawer title="重新发布" :visible.sync="drawer" :before-close="handleClose">
      <el-form>
        <el-form-item>
          <el-radio>发布上线</el-radio>
          <el-radio>课程下架</el-radio>
        </el-form-item>
      </el-form>
      <div>
        <el-button>取消</el-button>
        <el-button>确定</el-button>
      </div>
    </el-drawer>

    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <!-- 查看评价   -->
    <comment v-if="commentVisible" ref="comment" @refreshDataList="getDataList"></comment>
    <!--    <新增课程>-->
    <addCurriculum ref="addCurriculum" :getDataList="getDataList"></addCurriculum>
  </div>
</template>

<script>
import AddOrUpdate from './learn-vurriculum-add'
import Comment from './comment'
import AddCurriculum from '@/views/common/learntop'

export default {
  data() {
    return {
      visible: false,
      drawer: false,
      keyword: '',
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      commentVisible: false,
      adddrawerVisible: false,
      addOrUpdateVisible: false
    }
  },
  components: {
    AddOrUpdate,
    Comment,
    AddCurriculum
  },
  mounted() {
    this.getDataList()
  },
  methods: {
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {})
    },
    handleClick() {
      alert('是否取消发布')
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/train/getTrainSubjectCurrList'),
        method: 'post',
        data: {
          page: this.pageIndex,
          limit: this.pageSize,
          keyword: this.keyword
        }
      }).then(res => {
        if (res && res.code === 200) {
          this.dataList = res.data.list
          this.totalPage = res.data.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },

    addOrCurriculum(id) {
      this.$refs['addCurriculum'].init(id)
    },
    commentHandle(id) {
      this.commentVisible = true
      this.$nextTick(() => {
        this.$refs.comment.init(id)
      })
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map(item => item.id)
      this.$confirm(`确定要进行删除操作吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/train/traincurriculum/delete'),
          method: 'post',
          data: { ids: ids }
        }).then(res => {
          if (res && res.code === 200) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.warning(res.msg)
          }
        })
      })
    }
  }
}
</script>

<style scoped>
</style>
